package com.meeting.master.manager.impl.base;

import cn.hutool.core.collection.CollectionUtil;

import com.meeting.master.manager.RoomInfoBaseDtoManager;
import com.meeting.master.manager.RoomInfoDtoManager;
import com.meeting.master.manager.converter.RoomInfoBaseDtoConverter;
import com.meeting.master.manager.converter.RoomInfoDtoConverter;
import com.meeting.master.manager.dto.RoomInfoBaseDto;
import com.meeting.master.manager.dto.RoomInfoDto;
import com.meeting.master.persist.dos.RoomInfo;
import com.meeting.master.persist.mapper.RoomInfoDao;
import com.vs.code.AutoGenerated;
import com.vs.ox.common.exception.ErrorCode;
import com.vs.ox.common.exception.IgnoredException;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@AutoGenerated(locked = true, uuid = "3e74f82f-8947-3985-b0e8-75e838926e8f")
public abstract class RoomInfoDtoManagerBaseImpl implements RoomInfoDtoManager {
    @AutoGenerated(locked = true)
    @Autowired
    private RoomInfoBaseDtoConverter roomInfoBaseDtoConverter;

    @AutoGenerated(locked = true)
    @Autowired
    private RoomInfoBaseDtoManager roomInfoBaseDtoManager;

    @AutoGenerated(locked = true)
    @Autowired
    private RoomInfoDao roomInfoDao;

    @AutoGenerated(locked = true)
    @Autowired
    private RoomInfoDtoConverter roomInfoDtoConverter;

    @AutoGenerated(locked = true, uuid = "3a1057c2-64fb-3801-a217-147816de7604")
    @Override
    public List<RoomInfoDto> getByIds(List<Long> id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(id)) {
            return Collections.emptyList();
        }

        List<RoomInfo> roomInfoList = roomInfoDao.getByIdsForRoomInfoDto(id);
        if (CollectionUtil.isEmpty(roomInfoList)) {
            return Collections.emptyList();
        }

        // 结果和入参顺序保持一致
        Map<Long, RoomInfo> roomInfoMap =
                roomInfoList.stream().collect(Collectors.toMap(i -> i.getId(), i -> i));
        roomInfoList =
                id.stream()
                        .map(i -> roomInfoMap.get(i))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList());

        return doConvertFromRoomInfoToRoomInfoDto(roomInfoList);
        /** This block is generated by vs, do not modify, end anchor 1 */
    }

    @AutoGenerated(locked = true, uuid = "b87f8284-9740-3483-ab29-f311aeb2005d")
    public List<RoomInfoDto> doConvertFromRoomInfoToRoomInfoDto(List<RoomInfo> roomInfoList) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        if (CollectionUtil.isEmpty(roomInfoList)) {
            return Collections.emptyList();
        }

        List<RoomInfoBaseDto> baseDtoList =
                roomInfoList.stream()
                        .map(
                                tmp ->
                                        roomInfoBaseDtoConverter
                                                .convertFromRoomInfoToRoomInfoBaseDto(tmp))
                        .collect(Collectors.toList());
        Map<Long, RoomInfoDto> dtoMap =
                roomInfoDtoConverter.convertFromRoomInfoBaseDtoToRoomInfoDto(baseDtoList).stream()
                        .collect(
                                Collectors.toMap(
                                        RoomInfoDto::getId, Function.identity(), (o1, o2) -> o1));

        List<RoomInfoDto> roomInfoDtoList = new ArrayList<>();
        for (RoomInfo i : roomInfoList) {
            RoomInfoDto roomInfoDto = dtoMap.get(i.getId());
            if (roomInfoDto == null) {
                continue;
            }

            /** This block is generated by vs, do not modify, end anchor 1 */

            /** This block is generated by vs, do not modify, start anchor 2 */
            roomInfoDtoList.add(roomInfoDto);
        }
        return roomInfoDtoList;
        /** This block is generated by vs, do not modify, end anchor 2 */
    }

    @AutoGenerated(locked = true, uuid = "d6a2b6ac-2a1d-3067-b876-32df572f7886")
    @Override
    public RoomInfoDto getById(Long id) {
        /** This block is generated by vs, do not modify, start anchor 1 */
        List<RoomInfoDto> ret = getByIds(Arrays.asList(id));
        if (ret.size() > 1) {
            throw new IgnoredException(ErrorCode.SYS_ERROR, "返回值个数超过1个");
        }
        RoomInfoDto roomInfoDto = CollectionUtil.isEmpty(ret) ? null : ret.get(0);
        return roomInfoDto;
        /** This block is generated by vs, do not modify, end anchor 1 */
    }
}
